package boundarys;

import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import controllers.UpdateSaleController;
import controllers.initiateSaleController;
import entitys.MarketingPattern;
import entitys.Reply;
import enums.Command;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UpdateSaleGUI extends AbstractGUI{

	private JFrame frmCcrmupdateSale;
	private ArrayList<String> salesArr = new ArrayList<String>();
	private JComboBox salesComboBox;
	private JCheckBox SaleEnablesCheckBox;
	
	
	public UpdateSaleGUI() {
		initialize();
		initGUI(frmCcrmupdateSale);
		frmCcrmupdateSale.getContentPane().setLayout(null);
		
		UpdateSaleController.getSales();
		
		salesComboBox = new JComboBox();
		salesComboBox.setBounds(44, 38, 271, 20);
		frmCcrmupdateSale.getContentPane().add(salesComboBox);
		
		salesComboBox.addItemListener(new ItemListener() {		     
			@Override
			public void itemStateChanged(ItemEvent event) {
				if (event.getStateChange() == ItemEvent.SELECTED) {
					  
					int enabled=0;
			        int startindex,length;
			        String s=salesArr.get(salesComboBox.getSelectedIndex()).toString();
			       // System.out.println(s);
			        startindex=s.indexOf('@');
			        length=s.length();
			        
			      
			        enabled=Integer.parseInt(s.substring(startindex+1,length));
			        
			        if (enabled==1)
			        	SaleEnablesCheckBox.setSelected(true);
			        else
			        	SaleEnablesCheckBox.setSelected(false);
			        	
			        	
			      
			       }
				
			}
	    });
	   
		
		SaleEnablesCheckBox = new JCheckBox("Enable/Disable Sale");
		SaleEnablesCheckBox.setBounds(44, 100, 152, 23);
		frmCcrmupdateSale.getContentPane().add(SaleEnablesCheckBox);
		
		JButton btnUpdateSale = new JButton("Update Sale");
		btnUpdateSale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String s;
				int active;
				
				if (SaleEnablesCheckBox.isSelected()==true)
					active=1;
				else
					active=0;				
				s=new String(salesComboBox.getSelectedItem().toString()+"@"+active);
				
				UpdateSaleController.updateSale(s);
				
				JOptionPane.showMessageDialog(frmCcrmupdateSale, "Sale updated!", "ccrm->Update Sale", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		btnUpdateSale.setBounds(202, 94, 113, 35);
		frmCcrmupdateSale.getContentPane().add(btnUpdateSale);
		
		JLabel lblPickASale = new JLabel("Pick a sale to update");
		lblPickASale.setBounds(122, 13, 135, 14);
		frmCcrmupdateSale.getContentPane().add(lblPickASale);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmCcrmupdateSale = new JFrame();
		frmCcrmupdateSale.setTitle("CCRM>Update Sale");
		frmCcrmupdateSale.setResizable(false);
		frmCcrmupdateSale.setBounds(100, 100, 367, 206);
		frmCcrmupdateSale.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frmCcrmupdateSale.setVisible(true);
	}

	@Override
	public void getReply(Object r) {
		// TODO Auto-generated method stub
		
		if(((Reply)r).command.equals(Command.GET_SALES)) //if GET_PRODUCTS reply is received
		{
			salesArr =(ArrayList<String>) ((Reply)r).result;
			
			for (int i=0;i<salesArr.size();i++)
			{
				int indexOfS,saleID;
				
				indexOfS=salesArr.get(i).indexOf('@');
				saleID=Integer.parseInt(salesArr.get(i).substring(0,indexOfS));
				//type=Integer.parseInt(str.substring(indexOfS+1,str.length()));
				salesComboBox.addItem("#"+saleID);
				
			}
			
			
			
			String s=salesArr.get(0).toString();
			int enabled=0;
	        int startindex,length;
	        startindex=s.indexOf('@');
	        length=s.length();  
	        
	        enabled=Integer.parseInt(s.substring(startindex+1,length));
	        
	        
	        if (enabled==1)
	        	SaleEnablesCheckBox.setSelected(true);
	        else
	        	SaleEnablesCheckBox.setSelected(false);
	        	
	        
	       

		}
		
	}
}
